package com.shop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;

public class Step1 extends Configured implements Tool {
    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new Step1(), args));
    }

    @Override
    public int run(String[] strings) throws Exception {
        Configuration conf = getConf();
        // 指定输入输出路径
        Path step1input = new Path(
                "hdfs://192.168.10.11:9000/shopdata");
        Path step1output = new Path(
                "hdfs://192.168.10.11:9000/shop1output");

        FileSystem fs = FileSystem.get(
                new URI("hdfs://192.168.10.11:9000")
                , conf);
        if (fs.exists(step1output)) fs.delete(step1output, true);
        //构建Job
        Job job = Job.getInstance(conf);
        job.setJobName("step1");
        job.setJarByClass(this.getClass());

        job.setMapperClass(Step1Mapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(DoubleWritable.class);

        job.setReducerClass(Shop1Reduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DoubleWritable.class);
//        job.setNumReduceTasks(2);
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job, step1input);
        job.setOutputFormatClass(SequenceFileOutputFormat.class);
        SequenceFileOutputFormat.setOutputPath(job, step1output);
        return job.waitForCompletion(true) ? 0 : -1;
    }

    static class Step1Mapper extends Mapper<LongWritable, Text, Text, DoubleWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, DoubleWritable>.Context context) throws IOException, InterruptedException {
            String[] split = value.toString().split(",");
            String k2 = split[0] + "," + split[1];
            double num = 0;
            if (split[2].equals("showProduct")) {
                num = 0.05;
            } else if (split[2].equals("paySuccess")) {
                num = 0.5;
            } else if (split[2].equals("addCart")) {
                num = 0.15;
            } else if (split[2].equals("createOrder")) {
                num = 0.3;
            }
            context.write(new Text(k2), new DoubleWritable(num));
        }
    }

    static class Shop1Reduce extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {
        @Override
        protected void reduce(Text key, Iterable<DoubleWritable> values, Reducer<Text, DoubleWritable, Text, DoubleWritable>.Context context) throws IOException, InterruptedException {
            BigDecimal sum = new BigDecimal(0);
            for (DoubleWritable value : values) {
                sum =sum.add(BigDecimal.valueOf(value.get()));
            }
            context.write(key,new DoubleWritable(sum.doubleValue()));
//            Double sum = 0.0;
//            for (DoubleWritable value : values) {
//                sum += value.get();
//            }
//            context.write(key, new DoubleWritable(sum));
        }
    }
}
